package com.hk.service.product.impl;

import java.math.BigDecimal;
import java.math.BigInteger;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.hk.commons.HqlFilter;
import com.hk.commons.entity.ProductRouteInfo;
import com.hk.commons.entity.product.ProductInfo;
import com.hk.dao.base.impl.BaseServiceImpl;
import com.hk.service.product.IProductRouteInfo;

@Service
public class ProductRouteInfoImpl extends BaseServiceImpl<ProductRouteInfo>
		implements IProductRouteInfo {

	private Logger logger = LoggerFactory.getLogger(ProductRouteInfoImpl.class);

	@Autowired
	private ProductInfoImpl hkproductServer;

	@Resource
	private JdbcTemplate jdbcTemplate;

	/**
	 * 在路由表中根据产品id, 获取公司路由ID
	 */
	@Override
	public Integer getConmpanyRouteId(Long productId) {
		// TODO Auto-generated method stub

		Integer companyRouteId = 0;

		HqlFilter hqlFilter = new HqlFilter();
		hqlFilter.addEQFilter("productId", productId);
		ProductRouteInfo model = getByFilter(hqlFilter);

		if (model != null) {
			companyRouteId = model.getCompanyRouteId();
		}
		return companyRouteId;
	}

	/**
	 * 通过产品id 查询路由表，获得产品信息
	 */
	public ProductInfo getInfoById(Long productId) {

		// Integer companyRouteId = getConmpanyRouteId(productId);

		ProductInfo prodInfo = hkproductServer.findById(productId);
		/*
		 * ProductInfo prodInfo = new ProductInfo(); if (companyRouteId ==
		 * ConstantRoute.COMPANY_HUAKANG) { prodInfo =
		 * hkproductServer.findById(productId); } else if (companyRouteId ==
		 * ConstantRoute.COMPANY_YINGMI) {
		 * 
		 * } else if (companyRouteId == ConstantRoute.COMPANY_HUOCHAI) {
		 * prodInfo = hkproductServer.findById(productId); }
		 */
		return prodInfo;
	}

    /**
     * 添加新的产品
     * return new product id
     */
    public Long addProduct(ProductInfo model) {
        Long result = 0L;

        // 查询是否有重复的名称或产品编码
        String sql = "SELECT count(*) FROM t_product_info where name = \""
                + model.getName()
                + "\" or proNo = \""
                + model.getProNo()
                + "\"";
        Integer size = jdbcTemplate.queryForObject(sql, Integer.class);

        // 如果产品名称或编码相同则返回false
        if (size > 0) {
            logger.error("产品名称 {} 或者编码 {} 已存在, 保存失败。", model.getName(), model.getProNo());
            return result;
        }

        Long productId = hkproductServer.add(model);
        if (productId != null) {
            ProductRouteInfo routeInfo = new ProductRouteInfo();
            routeInfo.setProductId(model.getId());
            routeInfo.setCompanyRouteId(model.getIssuerId());
            add(routeInfo);

            result = productId;
        }

        return result;
    }

	/**
	 * 更新产品信息
	 */
	public Boolean updateProduct(ProductInfo model) {

		Boolean result = false;

		// 查询是否有重复的名称或产品编码
		String sql = "SELECT count(*) FROM t_product_info where name = \""
				+ model.getName()
				+ "\" or proNo = \""
				+ model.getProNo()
				+ "\"";
		Integer size = jdbcTemplate.queryForObject(sql, Integer.class);

		// 如果产品名称或编码相同则返回false
		if (size > 1) {
			result = false;
			logger.info("产品名称或者编码已存在,更新失败。");
		} else {
			hkproductServer.update(model);
			result = true;
		}

		/*
		 * 
		 * 
		 * Integer companyRouteId = model.getIssuerId();
		 * 
		 * if (companyRouteId == ConstantRoute.COMPANY_HUAKANG) {
		 * hkproductServer.update(model); } else if (companyRouteId ==
		 * ConstantRoute.COMPANY_HUOCHAI) { hkproductServer.update(model); }
		 */
		return result;
	}

	/**
	 * 删除产品信息
	 */
	@Override
	public Boolean deleteByProductId(Long productId) {

		// 产品库中删除产品
		ProductRouteInfo model = findByProductId(productId);
		// Integer companyRouteId = model.getCompanyRouteId();

		ProductInfo productInfo = hkproductServer
				.findById(model.getProductId());
		hkproductServer.delete(productInfo);

		/*
		 * if (companyRouteId == ConstantRoute.COMPANY_HUAKANG) { ProductInfo
		 * productInfo = hkproductServer.findById(model.getProductId());
		 * hkproductServer.delete(productInfo); } else if (companyRouteId ==
		 * ConstantRoute.COMPANY_HUOCHAI) { ProductInfo productInfo =
		 * hkproductServer.findById(model.getProductId());
		 * hkproductServer.delete(productInfo); }
		 */
		// 路由表中删除路由信息
		ProductRouteInfo routeInfo = findByProductId(productId);
		delete(routeInfo);

		return true;
	}

	/**
	 * 根据ID 查询路由信息
	 */
	@Override
	public ProductRouteInfo findById(Long id) {
		return getById(id);
	}

	/**
	 * 根据产品ID 查询路由信息
	 */
	@Override
	public ProductRouteInfo findByProductId(Long productId) {
		HqlFilter hqlFilter = new HqlFilter();
		hqlFilter.addEQFilter("productId", productId);
		ProductRouteInfo model = getByFilter(hqlFilter);
		return model;
	}

	/**
	 * 添加产品路由新
	 */
	@Override
	public Integer add(ProductRouteInfo u) {
		// TODO Auto-generated method stub
		u.setCts(System.currentTimeMillis());
		return (Integer) super.save(u);
	}

	/**
	 * 根据条件获取产品
	 * 
	 * @param companyId
	 *            公司id
	 * @param prodTerm
	 *            产品期限
	 * @param rate
	 *            产品收益
	 * @return
	 */
	public ProductInfo getByInfo(Integer companyId, Integer prodTerm,
			BigDecimal rate){
		ProductInfo prod = hkproductServer.getByInfo(companyId, prodTerm, rate);
		return prod;
	}

}
